﻿using CaseManagerLibrary.Http;
using CaseManagerLibrary.Models;
using PSDLib.Ctrl;
using PSDLib.Models;
using PSDManager.SystemSettings;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PSDManager
{
    public partial class FrmAddTask : Form
    {
        public List<rm_room_info> SelectedRooms { get; set; }
        public List<dl_room_config> SelectedChannels { get; set; }
        public DateTime? StartTime { get; set; }
        public DateTime? EndTime { get; set; }
        public int ExtendTime { get; set; }
        public int StreamType { get; set; }
        public int TaskType { get; set; }
        public FrmAddTask(int taskType)
        {
            InitializeComponent();
            this.dtpEndDate.Enabled = false;
            SelectedRooms = new List<rm_room_info>();
            SelectedChannels = new List<dl_room_config>();
            cmbStreamType.SelectedIndex = 0;
            this.StartPosition = FormStartPosition.CenterParent;
            TaskType = taskType;
            this.palExtendTime.Visible = (taskType == 1);
            if (taskType == Convert.ToInt32(DownloadTaskCreateType.常规Routine))
            {
                this.lblTaskType.Text = "创建日常24小时下载任务";
                this.chkCaseOnly.Checked = true;
                this.chkEndTime.Checked = false;
            }
            else if (taskType == Convert.ToInt32(DownloadTaskCreateType.自定义时段))
            {
                this.lblTaskType.Text = "创建自定义时段下载任务";
                this.chkCaseOnly.Checked = false;
                this.chkEndTime.Checked = true;
            }
           
            this.txtExtendTime.Value = (taskType == 0 ? 0 : 5);
            GetRoom();
        }

        private void GetRoom()
        {
            var result = HttpHelper.Get<HResult<List<rm_room_info>>>(AppSettings.Setting.API.GetFreeRoom);

            if (result.success)
            {
                tvwFree.Nodes.Clear();
                foreach (var room in result.data)
                {
                    TreeNode node_task = new TreeNode();
                    node_task.Text = room.room_number;
                    node_task.Name = room.id;
                    node_task.Tag = room;
                    node_task.ImageIndex = 0;
                    node_task.SelectedImageIndex = 0;
                    tvwFree.Nodes.Add(node_task);

                    var result_channel = HttpHelper.Get<HResult<List<dl_room_config>>>(AppSettings.Setting.API.GetFreeChannel + "?roomNum=" 
                        + room.room_number.Split("-".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)[0]);

                    if (result_channel.success)
                    {
                        foreach (var channel in result_channel.data)
                        {
                            TreeNode node_channel = new TreeNode();
                            node_channel.Text = channel.channel_name;
                            node_channel.Name = channel.id;
                            node_channel.Tag = channel;
                            node_channel.ImageIndex = 1;
                            node_channel.SelectedImageIndex = 1;
                            node_task.Nodes.Add(node_channel);
                        }
                    }
                }
            }
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            SelectedRooms.Clear();
            SelectedChannels.Clear();

            foreach (TreeNode node in tvwSelected.Nodes)
            {
                SelectedRooms.Add((rm_room_info)node.Tag);

                foreach (TreeNode cnode in node.Nodes)
                {
                    SelectedChannels.Add((dl_room_config)cnode.Tag);
                }
            }

            if (SelectedChannels.Count == 0)
            {
                MessageBox.Show("请选择要下载的录像通道！");
                return;
            }

            //DateTime st;

            //if (DateTime.TryParse(dtpStartDate.Value.ToString("yyyy-MM-dd") + " " + txtStartTime.Text.Trim(), out st))
            //{
            //    StartTime = st;
            //}
            //else
            //{
            //    MessageBox.Show("下载开始时间设置不正确！");
            //    return;
            //}
            StartTime = dtpStartDate.Value;

            if (chkEndTime.Checked)
            {
                //DateTime et;
                //if (DateTime.TryParse(dtpEndDate.Value.ToString("yyyy-MM-dd") + " " + txtEndTime.Text.Trim(), out et))
                //{
                //    EndTime = et;
                //}
                //else
                //{
                //    MessageBox.Show("下载结束时间设置不正确！");
                //    return;
                //}
                EndTime = dtpEndDate.Value;
            }
            else
            {
                EndTime = null;
            }

            ExtendTime = Convert.ToInt32(txtExtendTime.Value);
            StreamType = cmbStreamType.SelectedIndex;

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            TreeNode node_room = null;

            if (tvwFree.SelectedNode.Level == 0)
            {
                node_room = tvwFree.SelectedNode;
            }
            else if (tvwFree.SelectedNode.Level == 1)
            {
                node_room = tvwFree.SelectedNode.Parent;
            }

            if (node_room != null)
            {
                TreeNode nr = new TreeNode();
                nr.Text = node_room.Text;
                nr.Tag = node_room.Tag;

                tvwSelected.Nodes.Add(nr);

                for (int i = node_room.Nodes.Count - 1; i >= 0; i--)
                {
                    TreeNode nc = new TreeNode();
                    nc.Text = node_room.Nodes[i].Text;
                    nc.Tag = node_room.Nodes[i].Tag;
                    nr.Nodes.Add(nc);
                }

                tvwSelected.ExpandAll();

                node_room.Remove();
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            for (int i = tvwSelected.Nodes.Count - 1; i >= 0; i--)
            {
                TreeNode sel_room;
                var sn = tvwFree.Nodes.Find(tvwSelected.Nodes[i].Name, false);
                if (sn.Length == 0)
                {
                    sel_room = new TreeNode();
                    sel_room.Text = tvwSelected.Nodes[i].Text;
                    sel_room.Name = tvwSelected.Nodes[i].Name;
                    sel_room.Tag = tvwSelected.Nodes[i].Tag;
                    sel_room.ImageIndex = 0;
                    sel_room.SelectedImageIndex = 0;
                    tvwFree.Nodes.Add(sel_room);
                }
                else
                {
                    sel_room = sn[0];
                }

                for (int j = tvwSelected.Nodes[i].Nodes.Count - 1; j >= 0; j--)
                {
                    TreeNode sel_channel = new TreeNode();
                    sel_channel.Text = tvwSelected.Nodes[i].Nodes[j].Text;
                    sel_channel.Name = tvwSelected.Nodes[i].Nodes[j].Name;
                    sel_channel.Tag = tvwSelected.Nodes[i].Nodes[j].Tag;
                    sel_channel.ImageIndex = 1;
                    sel_channel.SelectedImageIndex = 1;
                    sel_room.Nodes.Insert(0, sel_channel);

                    tvwSelected.Nodes[i].Nodes.RemoveAt(j);
                }

                if (tvwSelected.Nodes[i].Nodes.Count == 0)
                {
                    tvwSelected.Nodes.RemoveAt(i);
                }
                sel_room.Expand();
            }
        }
        int deep = 0;
        private void tvwFree_AfterCheck(object sender, TreeViewEventArgs e)
        {
            TreeViewCheck.CheckControl(e);
        }

        private void tvwSelected_AfterCheck(object sender, TreeViewEventArgs e)
        {
            TreeViewCheck.CheckControl(e);
        }

        private void chkEndTime_CheckedChanged(object sender, EventArgs e)
        {
            dtpEndDate.Enabled = chkEndTime.Checked;
        }
    }
}
